﻿package {
    import flash.display.*;
    import flash.events.*;
    import flash.net.*;
    import flash.ui.*;
    import flash.utils.*;

    import mx.core.*;

    import tagul.*;

    public class Cloud extends Sprite {
        public function Cloud(stage:Stage) {
            var ba:mx.core.ByteArrayAsset;
            var embeddedSVGString:String;
            var id:String;
            var params:Object;
            params = null;
            this.embeddedSVGClass = Cloud_embeddedSVGClass;
            super();


            ba = ByteArrayAsset(new this.embeddedSVGClass());
            embeddedSVGString = ba.readUTFBytes(ba.length);
            this.embeddedSVG = new XML(embeddedSVGString);
            params = stage.loaderInfo.parameters;
            id = "./1219@3";
            this.popup = params.popup != "true" ? false : true;
            stage.addEventListener(DrawStatusEvent.DONE, this.finalize);
            this.downloadXML(id);
            return;
        }

        private function finalize(arg1:DrawStatusEvent):void {
            this.backgroundBox = new Shape();
            this.backgroundBox.graphics.beginFill(this.tagulCloud.backgroundColor);
            this.backgroundBox.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
            this.backgroundBox.graphics.endFill();
            addChild(this.backgroundBox);
            addChild(this.tagulCloud);
            stage.addEventListener(Event.RESIZE, this.resizeHandler);
            this.tagulCloud.resize(stage.stageWidth, stage.stageHeight);
            return;
        }

        private function downloadXML(url:String):void {
            var loader:URLLoader = new URLLoader();
            var request:URLRequest;
            loader.dataFormat = "binary";
            loader.addEventListener(Event.COMPLETE, this.downloadCompleteHandler);
            loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, function(event:SecurityErrorEvent):void {
                //Alert.show("security error:"+event.text);
                });
            loader.addEventListener(IOErrorEvent.IO_ERROR, function(event:IOErrorEvent):void {
                //Alert.show("io error:"+event.text);
                });
            request = new URLRequest(url);
            try {
                loader.load(request);
            } catch (e:Error) {
                //Alert.show("error:"+e.message);
                trace(e.message);
                preDraw(embeddedSVG);
            }
            return;
        }

        private function downloadCompleteHandler(e:Event):void {
            var ba:ByteArray = null;
            var xml:XML = null;
            try {
                ba = URLLoader(e.target).data;
                try {
                    ba.uncompress();
                } catch (e:Error) {
                }
                xml = new XML(ba.toString());
                if (xml.localName().toLowerCase() != "svg") {
                    throw new Error("any text");
                }
            } catch (e:Error) {
                //Alert.show(e.message);
                xml = embeddedSVG;
            }
            this.preDraw(xml);
            return;
        }

        private function preDraw(arg1:XML):void {
            this.tagulCloud = new TagulCloud();
            this.tagulCloud.parseANDdraw(arg1, this.popup);
            dispatchEvent(new DrawStatusEvent("done"));
            return;
        }

        private function resizeHandler(arg1:flash.events.Event):void {
            this.backgroundBox.width = stage.stageWidth;
            this.backgroundBox.height = stage.stageHeight;
            this.tagulCloud.resize(stage.stageWidth, stage.stageHeight);
            return;
        }

        private var embeddedSVGClass:Class;

        private var embeddedSVG:XML = null;

        private var backgroundBox:flash.display.Shape;

        private var newWindowBox:flash.display.Sprite;

        private var tagulCloud:tagul.TagulCloud = null;

        private var popup:Boolean;
    }
}


